<?php

error_reporting(E_ALL);  // Turn on all errors, warnings and notices for easier debugging

global $answersArray;
global $designFlag;
$designFlag = False;

isset($_GET['arr']) ? $answersArray = $_GET['arr'] : die("Get Request Failed");

global $ascAnswersArray;
$ascAnswersArray['purpose'] = $answersArray[0];//options: simple, pro
$ascAnswersArray['measures'] = $answersArray[1];//options: small, medium, large
$ascAnswersArray['battery'] = $answersArray[2];//options: small, medium, big
$ascAnswersArray['design'] = $answersArray[3];//options: innovative, conservative
if ($ascAnswersArray['design'] == "innovative") $designFlag = True;
$ascAnswersArray['moneyLowerBound'] = $answersArray[4];
$ascAnswersArray['moneyUpperBound'] = $answersArray[5];
 
// API request variables
$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1';  // URL to call
$version = '1.0.0';  // API version supported by application
$appid = 'HenryAtY-eafb-4a4f-8574-c58742733e54';  // Given at the main developers screen
$globalid = 'EBAY-US';  // Global ID of the eBay site to search (e.g., EBAY-DE)
$AppleCategory = '111422'; //Apple Laptops
$PCcategory = '177'; //PC&Laptops
if ($designFlag == True) $category = '111422';
else $category = '177';
//$category = '177';

$i = '0';  // Initialize the item filter index to 0

// Create a PHP array of the item filters to use in the request
$filterarray =
  array(
    array(
    'name' => 'MaxPrice',
    'value' => $ascAnswersArray['moneyUpperBound'],
    'paramName' => 'Currency',
    'paramValue' => 'USD'),
    array(
    'name' => 'MinPrice',
    'value' => $ascAnswersArray['moneyLowerBound'],
    'paramName' => 'Currency',
    'paramValue' => 'USD'),
    /*array(
    'name' => 'FreeShippingOnly',
    'value' => 'true',
    'paramName' => '',
    'paramValue' => ''),*/
    array(
    'name' => 'ListingType',
    'value' => 'FixedPrice',
    'paramName' => '',
    'paramValue' => ''),
    array(
    'name' => 'buyItNowAvailable',
    'value' => 'true',
    'paramName' => '',
    'paramValue' => ''),
    array(
    'name' => 'Condition',
    'value' => 'New',
    'paramName' => '',
    'paramValue' => ''),
        
  );
  
// Generates an indexed URL snippet from the array of item filters
function buildURLArray ($filterarray) {
  global $urlfilter;
  global $i;
  // Iterate through each filter in the array
  foreach($filterarray as $itemfilter) {
    // Iterate through each key in the filter
    foreach ($itemfilter as $key =>$value) {
      if(is_array($value)) {
        foreach($value as $j => $content) { // Index the key for each value
          $urlfilter .= "&itemFilter($i).$key($j)=$content";
        }
      }
      else {
        if($value != "") {
          $urlfilter .= "&itemFilter($i).$key=$value";
        }
      }
    }
    $i++;
  }
  return "$urlfilter";
} // End of buildURLArray function

// Generates an indexed URL snippet from the array of aspect filters
function buildAspectFilterURLArray ($aspectfilterarray) {
  global $aspecturlfilter;
  global $i;
  // Iterate through each filter in the array
  foreach($aspectfilterarray as $aspectfilter) {
    // Iterate through each key in the filter
    foreach ($aspectfilter as $key =>$value) {
      if(is_array($value)) {
        foreach($value as $j => $content) { // Index the key for each value
          $aspecturlfilter .= "&aspectFilter($i).$key($j)=$content";
        }
      }
      else {
        if($value != "") {
          $aspecturlfilter .= "&aspectFilter($i).$key=$value";
        }
      }
    }
    $i++;
  }
  return "$aspecturlfilter";
} // End of buildURLArray function

/********************* configure the purpose of use **********************/

if ($ascAnswersArray['purpose'] == "simple"){
	$MacAspectFilterArray =
	array(
			array(
			'aspectName' => 'Product Family',
			'aspectValueName' => array('iBook', 'MacBook', 'MacBook Air'),
			'paramName' => '',
			'paramValue' => ''),
			array(
			'aspectName' => 'Processor Type',
			'aspectValueName' => array(
					'Intel Core 2 Duo',
					'Intel Core Duo',
					'Intel Core i3 1st Gen.',
					'Intel Core i3 2nd Gen.',
					'Intel Core i5 1st Gen.',
					'Intel Core i5 2nd Gen.',
					'Intel Core i5 3rd Gen.',
					'Intel Core i5 4th Gen.',
					'PowerPC G3',
					'PowerPC G4'),
			'paramName' => '',
			'paramValue' => ''),
	);
	$aspectfilterarray =
	array(
	    array(
	    'aspectName' => 'Type',
	    'aspectValueName' => array('Ultrabook', 'Netbook', 'Tablet/Laptop Convertible'),
	    'paramName' => '',
	    'paramValue' => ''),
	    array(
	    'aspectName' => 'Product Line',
	    'aspectValueName' => array(
	    	'Eee', 
	    	'IdeaPad', 
	    	'Inspiron', 
	    	'Lifebook', 
	    	'Mini', 
	    	'ThinkPad', 
	    	'ProBook', 
	    	'Satellite', 
	    	'Studio',
	    	'U Series', 
	    	'VAIO'),
	    'paramName' => '',
	    'paramValue' => ''),
	    array(
	    'aspectName' => 'Processor Type',
	    'aspectValueName' => array(
			'AMD Athlon',
			'AMD Athlon 64 X2',
			'Intel Atom',
			'Intel Celeron',
			'Intel Celeron M',
			'Intel Core 2',
			'Intel Core 2 Duo',
			'Intel Core 2 Quad',
			'Intel Core Duo',
			'Intel Core i3 1st Gen.',
			'Intel Core i3 2nd Gen.',
			'Intel Core i3 3rd Gen.',
			'Intel Core i3 4th Gen.',
			'Intel Core i5 1st Gen.',
			'Intel Core i5 2nd Gen.',
			'Intel Core i5 3rd Gen.',
			'Intel Core i5 4th Gen.'),
	    'paramName' => '',
	    'paramValue' => ''),    
  	);
} else {
	$MacAspectFilterArray =
	array(
			array(
			'aspectName' => 'Product Family',
			'aspectValueName' => array('MacBook Pro', 'PowerBook'),
			'paramName' => '',
			'paramValue' => ''),
			array(
			'aspectName' => 'Processor Type',
			'aspectValueName' => array(
					'Intel Core i7 1st Gen.',
					'Intel Core i7 2nd Gen.',
					'Intel Core i7 3rd Gen.',
					'Intel Core i7 4th Gen.'),
			'paramName' => '',
			'paramValue' => ''),
	);
	$aspectfilterarray =
	array(
	    array(
	    'aspectName' => 'Type',
	    'aspectValueName' => array('Notebook', 'Ultrabook'),
	    'paramName' => '',
	    'paramValue' => ''),
	    array(
	    'aspectName' => 'Product Line',
	    'aspectValueName' => array(
	    	'Aspire One',
	    	'Latitude', 
	    	'NV Series', 
	    	'Inspiron', 
	    	'Pavilion', 
	    	'Precision', 
	    	'Presario', 
	    	'Qosmio', 
	    	'Tecra' , 
	    	'Toughbook', 
	    	'Travelmate', 
	    	'XPS'),
	    'paramName' => '',
	    'paramValue' => '' ),
		array(
		'aspectName' => 'Processor Type',
		'aspectValueName' => array(
			'Intel Core i7 1st Gen.',
			'Intel Core i7 2nd Gen.',
			'Intel Core i7 3rd Gen.',
			'Intel Core i7 4th Gen.',
			'AMD Athlon II',
			'AMD Athlon ll',
			'AMD Athlon Neo',
			'AMD E-Series',
			'AMD Sempron',
			'AMD Turion ll',
			'AMD Turion X2',
			'Intel Pentium',
			'Intel Pentium Mobile',
			'Intel Xeon',
			'Not Specified'),
		'paramName' => '',
		'paramValue' => ''),
		);
}
/*************** end of "purpose of use" **************************/

/********************* measurements *******************************/
if ($ascAnswersArray['measures'] == "small"){
	array_push($MacAspectFilterArray,
		array(
		'aspectName' => 'Product Family',
	    'aspectValueName' => array(
			'iBook',
			'MacBook Air'),
	    'paramName' => '',
	    'paramValue' => ''),
		array(
		'aspectName' => 'Screen Size',
		'aspectValueName' => array(
				'11 inches',
				'12 inches'),
		'paramName' => '',
		'paramValue' => '')
	);
	array_push($aspectfilterarray,
		array(
	    'aspectName' => 'Screen Size',
	    'aspectValueName' => array(
			'14-14.9 inches',
			'12-12.9 inches',
			'Less than 12 inches'),
	    'paramName' => '',
	    'paramValue' => '')
  );
} elseif ($ascAnswersArray['measures'] == "medium") {
	array_push($MacAspectFilterArray,
		array(
		'aspectName' => 'Screen Size',
		'aspectValueName' => array(
			'13 inches',
			'14 inches'),
		'paramName' => '',
		'paramValue' => '')
	);
	array_push($aspectfilterarray,
		array(
	    'aspectName' => 'Type',
	    'aspectValueName' => 'Ultrabook',
	    'paramName' => '',
	    'paramValue' => ''),
	    array(
	    'aspectName' => 'Screen Size',
	    'aspectValueName' => array(
			'15-15.9 inches',
			'16-16.9 inches'),
	    'paramName' => '',
	    'paramValue' => '')
	);
} else {
	array_push($MacAspectFilterArray,
		array(
		'aspectName' => 'Product Family',
		'aspectValueName' => array(
			'MacBook','MacBook Pro','PowerBook'),
		'paramName' => '',
		'paramValue' => ''),
		array(
		'aspectName' => 'Screen Size',
		'aspectValueName' => array(
			'15 inches',
			'17 inches'),
		'paramName' => '',
		'paramValue' => '')
	);
	array_push($aspectfilterarray,
	    array(
	    'aspectName' => 'Screen Size',
	    'aspectValueName' => '17 inches or more',
	    'paramName' => '',
	    'paramValue' => ''),
	    array(
	    'aspectName' => 'Hard Drive Capacity',
	    'aspectValueName' => '500 GB or more',
	    'paramName' => '',
	    'paramValue' => ''),
	    array(
	    'aspectName' => 'Memory',
	    'aspectValueName' => '8 GB or more',
	    'paramName' => '',
	    'paramValue' => '')
	);
}
/*************** end of "measurements" **************************/

/************************** battery *****************************/
if ($ascAnswersArray['battery'] == "big"){
	array_push($MacAspectFilterArray,
		array(
		'aspectName' => 'Product Family',
		'aspectValueName' => array(
		'MacBook','MacBook Air','iBook'),
		'paramName' => '',
		'paramValue' => ''),
		array(
		'aspectName' => 'Screen Size',
		'aspectValueName' => array(
			'11 inches',
			'12 inches'),
		'paramName' => '',
		'paramValue' => '')
	);
	array_push($aspectfilterarray,
		array(
	    'aspectName' => 'Type',
	    'aspectValueName' => array(
			'Tablet/Laptop Convertible',
			'Netbook'),
	    'paramName' => '',
	    'paramValue' => '')
  	);
} elseif ($ascAnswersArray['battery'] == "medium") {
	array_push($MacAspectFilterArray,
		array(
		'aspectName' => 'Product Family',
		'aspectValueName' => array(
			'MacBook',
			'MacBook Air', 
			'MacBook Pro'),
		'paramName' => '',
		'paramValue' => ''),
		array(
		'aspectName' => 'Screen Size',
		'aspectValueName' => array(
			'13 inches',
			'14 inches'),
		'paramName' => '',
		'paramValue' => '')
	);
	array_push($aspectfilterarray,
		array(
	    'aspectName' => 'Type',
	    'aspectValueName' => 'Ultrabook',
	    'paramName' => '',
	    'paramValue' => '')
	);
} else {
	array_push($MacAspectFilterArray,
	array(
		'aspectName' => 'Product Family',
		'aspectValueName' => array(
			'MacBook',
			'MacBook Pro',
			'PowerBook'),
		'paramName' => '',
		'paramValue' => ''),
		array(
		'aspectName' => 'Screen Size',
		'aspectValueName' => array(
			'15 inches',
			'17 inches'),
		'paramName' => '',
		'paramValue' => '')
	);
	array_push($aspectfilterarray,
	    array(
	    'aspectName' => 'Type',
	    'aspectValueName' => 'Notebook',
	    'paramName' => '',
	    'paramValue' => ''),
	    array(
		'aspectName' => 'Processor Type',
		'aspectValueName' => array(
			'Intel Core i7 1st Gen.',
			'Intel Core i7 2nd Gen.',
			'Intel Core i7 3rd Gen.',
			'Intel Core i7 4th Gen.',
			'AMD Athlon II',
			'AMD Athlon ll',
			'AMD Athlon Neo',
			'AMD E-Series',
			'AMD Sempron',
			'AMD Turion ll',
			'AMD Turion X2',
			'Intel Pentium',
			'Intel Pentium Mobile',
			'Intel Xeon',
			'Not Specified'),
		'paramName' => '',
		'paramValue' => ''),
	    array(
	    'aspectName' => 'Graphics Processing Type',
	    'aspectValueName' => 'Dedicated Graphics',
	    'paramName' => '',
	    'paramValue' => '')
	);
}
/*************** end of "measurements" **************************/

/******************* Design ************************************/
if ($ascAnswersArray['design'] == "innovative"){
	array_push($aspectfilterarray,
		array(
	    'aspectName' => 'Product Line',
	    'aspectValueName' => array(
			'EliteBook',
			'IdeaPad'),
	    'paramName' => '',
	    'paramValue' => ''),
	    array(
	    'aspectName' => 'Graphics Processing Type',
	    'aspectValueName' => 'Hybrid Graphics',
	    'paramName' => '',
	    'paramValue' => '')
  );
} else {
	array_push($aspectfilterarray,
		array(
	    'aspectName' => 'Type',
	    'aspectValueName' => array('Notebook','Netbook'),
	    'paramName' => '',
	    'paramValue' => '')	    
	);
}
/***************** end of Design ********************************/


// Build the indexed item filter and aspect filter URL snippet
buildURLArray($filterarray);
$i=0;
if ($designFlag == False) buildAspectFilterURLArray($aspectfilterarray);
else buildAspectFilterURLArray($MacAspectFilterArray);

// // Construct the findItemsCategory HTTP GET call
// $apicall = "$endpoint?";
// $apicall .= "OPERATION-NAME=findItemsAdvanced";
// $apicall .= "&SERVICE-VERSION=$version";
// $apicall .= "&SECURITY-APPNAME=$appid";
// $apicall .= "&RESPONSE-DATA-FORMAT=JSON";
// $apicall .= "&callback=_cb_findItemsAdvancedResponse";
// $apicall .= "&REST-PAYLOAD";
// $apicall .= "&categoryId=$category";
// $apicall .= "$urlfilter";
// $apicall .= "$aspecturlfilter";

// echo $apicall; // Return API call



// // Build the indexed item filter and aspect filter URL snippet
// buildURLArray($filterarray);
// $i=0;
// if (designFlag == True) {
// 	buildAspectFilterURLArray($MacAspectFilterArray);
// } else buildAspectFilterURLArray($aspectfilterarray);

// Construct the findItemsCategory HTTP GET call
$apicall = "$endpoint?";
$apicall .= "OPERATION-NAME=findItemsAdvanced";
$apicall .= "&SERVICE-VERSION=$version";
$apicall .= "&SECURITY-APPNAME=$appid";
$apicall .= "&RESPONSE-DATA-FORMAT=JSON";
$apicall .= "&callback=_cb_findItemsAdvancedResponse";
$apicall .= "&REST-PAYLOAD";
$apicall .= "&categoryId=$category";
$apicall .= "$urlfilter";
$apicall .= "$aspecturlfilter";

// $PCapicall = $apicall."&categoryId=$PCcategory";
// $PCapicall .= "$urlfilter";
// $PCapicall .= "$aspecturlfilter";

// $AppleApicall = $apicall."&categoryId=$AppleCategory";
// $AppleApicall .= "$urlfilter";
// $AppleApicall .= "$aspecturlfilter";



//if ($designFlag == False) echo $PCapicall;
echo $apicall; // Return API call

// echo $PCapicall;

?>
